package com.ljkj.cordial.chat.ui.views;

import com.lanjiu.lib.imclient.bus.EventConstants;
import com.lanjiu.lib.imclient.bus.MessageEvent;
import com.lanjiu.lib.imclient.dto.SettingsDto;
import com.lanjiu.lib.imclient.dto.base.Dto;
import com.lanjiu.lib.imclient.user.UserPreferences;
import com.lanjiu.lib.utils.log.Logger;
import com.ljkj.cordial.chat.ui.IView;

/**
 * @description:
 * @author: Panwei
 * @date: 2019/12/5 13:53
 */
public class SysSettingView extends IView {

    private SysSettingCallback mCallback;

    public SysSettingView(SysSettingCallback mCallback) {
        this.mCallback = mCallback;
    }

    public void doGetSettingsInfo() {
        doSendRequest(EventConstants.Action.PERSONAL_SETTINGS_INFO);
    }

    public void doSetOfflineMsg(boolean isOpen) {
        int action = UserPreferences.isRegistered() ? EventConstants.Action.PERSONAL_OFFLINE_MSG_REGISTERED : EventConstants.Action.PERSONAL_OFFLINE_MSG_TOURISTS;
        doSendRequest(action, String.valueOf(isOpen));
    }

    public void doSetExitClean(boolean isOpen) {
        int action = UserPreferences.isRegistered() ? EventConstants.Action.PERSONAL_EXIT_CLEAN_REGISTERED : EventConstants.Action.PERSONAL_EXIT_CLEAN_TOURISTS;
        doSendRequest(action, String.valueOf(isOpen));
    }

    public void doSetTimerClean(int flg) {
        int action = UserPreferences.isRegistered() ? EventConstants.Action.PERSONAL_TIMER_CLEAN_REGISTERED : EventConstants.Action.PERSONAL_TIMER_CLEAN_TOURISTS;
        doSendRequest(action, String.valueOf(++flg));
    }

    @Override
    protected void onMessageEventHandle(MessageEvent event) {
        Logger.print("SysSettingView", event.toString());
        if (event.action() == EventConstants.Action.PERSONAL_SETTINGS_INFO) {
            SettingsDto settingsDto = null;
            if (checkIsSuccess(event.code(), event.data()))
                settingsDto = Dto.fromJson(event.data(), SettingsDto.class);
            if (null != mCallback)
                mCallback.onSettingsInfoCallback(event.code(), event.msg(), settingsDto);
        } else if (event.action() == EventConstants.Action.PERSONAL_OFFLINE_MSG_REGISTERED ||
                event.action() == EventConstants.Action.PERSONAL_OFFLINE_MSG_REGISTERED) {
            if (null != mCallback)
                mCallback.onSetOfflineMsgCallback(event.code(), event.msg());
        } else if (event.action() == EventConstants.Action.PERSONAL_EXIT_CLEAN_TOURISTS ||
                event.action() == EventConstants.Action.PERSONAL_EXIT_CLEAN_REGISTERED) {
            if (null != mCallback)
                mCallback.onSetExitCleanCallback(event.code(), event.msg());
        } else if (event.action() == EventConstants.Action.PERSONAL_TIMER_CLEAN_TOURISTS ||
                event.action() == EventConstants.Action.PERSONAL_TIMER_CLEAN_REGISTERED) {
            if (null != mCallback)
                mCallback.onSetTimerCleanCallback(event.code(), event.msg());
        }
    }

    public interface SysSettingCallback {

        void onSettingsInfoCallback(int code, String msg, SettingsDto data);

        void onSetOfflineMsgCallback(int code, String msg);

        void onSetExitCleanCallback(int code, String msg);

        void onSetTimerCleanCallback(int code, String msg);
    }
}
